import * as usersService from '../services/users';
import { routerRedux } from 'dva/router';
import { message, notification } from 'antd';

export default {
  namespace: 'users', 
  state: {
    user:{},
    token:{}
   
  },
  reducers: {
    save(state, { payload: { user } }) {
      return { ...state, user };
    },
  },
  effects: {
    *login({ payload: { userName,password},callback }, { call, put }) {
      try {
      const {  token } = yield call(usersService.login, { loginname: userName, password });
      yield put({
        type: 'save',
        payload: {
          token
        },
      });
      localStorage.setItem('TOKEN', token)
      yield put(routerRedux.push('/sign'));
      } catch (e) {
        message.error(e.message, 3);
      }
    },

    *getUserInfo(_, { call, put }) {
      const { userInfo:user} = yield call(usersService.getUserInfo);
      // console.log(user,"xxxxxx===");
      yield put({
        type: 'save',
        payload: {
          user,
        },
      });
    },

    *sign({payload:{signTime}}, { call, put }) {
      try {
        const {userInfo} = yield call(usersService.sign, { signTime });
        yield put({
          type: 'save',
          payload: {
            user: userInfo
          },
        });
        notification['success']({
          message: 'sign up success, enjoy',
  
        });
    
      } catch (e) {
        message.error(e.message, 3);
      }
     
    },
    *reset(_,{call,put}){
      const { userInfo: user } = yield call(usersService.resetUser);
      yield put({
        type: 'save',
        payload: {
          user,
        },
      });

    }

    

  },
  subscriptions: {
   
  },
};
